package edu.xust.MetadataGenerator.service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

import edu.xust.MetadataGenerator.domain.Field;

public class FieldParserImpl implements FieldParser {

	public List<Field> load(String fileName){
		return load(fileName, "\t");
	}
	
	public List<Field> load(String fileName, String delemiter) {
		BufferedReader  br = null;
		List<Field> fields = new LinkedList<Field>();
		try {
			FileInputStream fis = new FileInputStream(fileName);
			br = new BufferedReader(new InputStreamReader(fis, "GB2312"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		if (null != br) {
			try {
				String line = br.readLine();
				while (line != null) {
					String[] arr = line.split(delemiter);
					if(arr.length >= 3){
						Field field = new Field();
						field.setName(arr[0]);
						field.setLabel(arr[1]);
						field.setComment(arr[1]);
						field.setType(FieldParser.DataType.toType(arr[2].trim()));
						fields.add(field);
					}
					line = br.readLine();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return fields;
	}

}
